//
//  ChangePassVC.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/9.
//

#import "ChangePassVC.h"
#import "LoginPageViewController.h"

@interface ChangePassVC ()

@end

@implementation ChangePassVC

-(void)viewWillAppear:(BOOL)animated{
     [super viewWillAppear:animated];

   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self layoutCustomNav:self.view title:@"修改密码"];
    
    [self.sureButton createBordersWithColor:nil withCornerRadius:5.0 andWidth:0];
    [self.sureButton whenTapped:^{
        [self commitUpdateInfo];
    }];
    [self.view whenTapped:^{
        [self.view endEditing:YES];
    }];
   
}
-(void)commitUpdateInfo{
    [self.view endEditing:YES];
   
    if (self.oldPwTF.text.length == 0 ) {
        showAlert(@"原密码不能为空");
        return;
    }
    if (self.passTF.text.length == 0 || self.surePassTF.text.length == 0) {
        showAlert(@"新密码或者确认密码不能为空");
        return;
    }
    if (![self.passTF.text isEqualToString:self.surePassTF.text]) {
        showAlert(@"新密码和确认密码不一致");
        return;
    }
    if (self.passTF.text.length == 0||self.passTF.text.length<8){
        showAlert(@"密码不能为空或长度不能小于8位");
        return;
    }
    if (self.passTF.text.length>18) {
        showAlert(@"密码长度不能大于18位");
        return;
    }
    if ([self.passTF.text isValidPwd] == NO) {
        showAlert(@"密码必须为数字英文混合设置");
        return;
    }

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@"3" forKey:@"cmd"];
    [params setObject:self.passTF.text forKey:@"password"];
    [params setObject:self.oldPwTF.text forKey:@"oldpwd"];
    
    
    
    
    [MSNetworkManager postRevertUrl:Post_UpDateUserInfo params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
            [[SDWebImageDownloader sharedDownloader] cancelAllDownloads];
            [[YTKNetworkAgent sharedAgent] cancelAllRequests];
            [SSKeyChainMgr delLoginToken];
            [YLUserMgr deleteUser];
            LoginPageViewController *pageVC = [[LoginPageViewController  alloc] init];
            UINavigationController *navigationVC = [[UINavigationController alloc] initWithRootViewController:pageVC];
            UIWindow *mainWindow = [BaseViewController getCurrentWindow];
            [mainWindow setRootViewController:navigationVC];
            showAlert(@"密码更改成功，请重新进行登录");
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    

}


@end
